package com.qlslylq.ad.example.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;

import com.qlslylq.ad.example.R;
import com.qlslylq.ad.example.constant.KeyConst;
import com.qlslylq.ad.sdk.core.listener.SplashAdListener;
import com.qlslylq.ad.sdk.core.loader.SplashAdLoader;
import com.qlslylq.ad.sdk.core.rsp.AdError;
import com.qlslylq.ad.sdk.util.DeviceUtil;
import com.qlslylq.ad.sdk.util.Log;

/**
 * 开屏广告测试页<br/>
 */
public class SplashAdTestMainActivity extends BaseActivity {

    private SplashAdLoader loader;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_ad_test_main);
    }

    protected void initView() {
        setTitle("开屏广告测试");
    }

    protected void initData() {

    }

    protected void initListener() {

    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_0:
                reverseScreenOrientation();
                break;
            case R.id.btn_1:
                initLoaderAndLoad();
                break;
            case R.id.btn_2:
                routeToSplashAdTestActivity(false);
                break;
            case R.id.btn_3:
                routeToSplashAdTestActivity(true);
                break;
            case R.id.btn_4:
                routeToSplashAdTestActivity(false);
                break;
            case R.id.btn_5:
                routeToSplashAdTestActivity(true);
                break;
            case R.id.btn_6:
                showToast("正在开发中~");
                break;
        }
    }

    private void initLoaderAndLoad() {
        //不带adContainer的构造仅用于存在前置页面时的预加载，不能用于展示；且事件均不会回调，保持空实现即可
        //不存在前置页面时，即在Splash展示页面中的加载展示请使用带adContainer的构造
        if (loader == null) {
            Log.d(String.format("预加载开屏广告物料的尺寸为：width = %s，height = %s", DeviceUtil.getScreenWidth(this), DeviceUtil.getScreenHeight(this)));
            loader = new SplashAdLoader(this, KeyConst.JH_SPLASH_POS_ID, DeviceUtil.getScreenWidth(this), DeviceUtil.getScreenHeight(this), 5 * 1000, new SplashAdListener() {
                public void onAdShow() {

                }

                public void onAdShowError(AdError error) {

                }

                public void onAdDismiss() {

                }

                public void onAdClick() {

                }
            });
        }
        loader.load();
    }

    /**
     * 路由<br/>
     */
    private void routeToSplashAdTestActivity(boolean fullScreen) {
        boolean isLandscape = getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        Intent intent = new Intent(this, isLandscape ? SplashAdTestLActivity.class : SplashAdTestActivity.class);
        intent.putExtra("fullScreen", fullScreen);
        intent.putExtra("isLandscape", isLandscape);
        startActivity(intent);
    }

    /**
     * 横竖屏切换<br/>
     */
    private void reverseScreenOrientation() {
        setRequestedOrientation(
                getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
                        ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
                        : ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
}